#!/bin/bash

# echo "start .bashrc"echo -e "${HLIGHT}---${HOME}/.bashrc---${NORMAL}" #start files

bios_check_dot_pathes() { #? $1 UP (PLT) $2 down (plt) $3 rootdir (${PATH_CURR_REPO} , ${HOME}/REPOBARE/)
    if [ -f "${HOME}/.${2}_path" ]; then
        ${1}_PATH=$(cat "${HOME}/.${2}_path")
    fi

    if [ -z "$1" ] || ! [ "$(basename ${1}_PATH)" == "${2}" ]; then
        echo "PLT_PATH not avalable" >&2
        unset y
        read -p "true?: ${1}_PATH=${3}/${2}" y
        if [[ $y == "y" ]]; then
            echo ${HOME}/REPOBARE/${2} >"${HOME}/.${2}_path"
        else
            echo "${1}_PATH not avalable create ${HOME}/.${2}_path with path to ${2}; return 1" >&2
            return 1
        fi
    fi
}

unset FONSH_PATH
bios_check_dot_pathes "FONSH" "fonsh" "${PATH_CURR_REPO}" || return 1

# if [ -f "${HOME}/.fonsh_path" ]; then
#     FONSH_PATH=$(cat "${HOME}/.fonsh_path")
# fi

# if [ -z "${FONSH_PATH}" ] || ! [ "$(basename $FONSH_PATH)" == "fonsh" ]; then
#     echo "FONSH_PATH not avalable" >&2
#     unset y
#     read -p "true?: FONSH_PATH=${FONSH_PATH}" y
#     if [[ $y == "y" ]]; then
#         echo ${HOME}/REPOBARE/fonsh >"${HOME}/.fonsh_path"
#     else
#         echo "FONSH_PATH not avalable create ${HOME}/.fonsh_path with path to fonsh; return 1" >&2
#         return 1
#     fi
# fi



unset PLT_PATH
bios_check_dot_pathes "PLT" "plt" "${PATH_CURR_REPO}" || return 1

# if [ -f "${HOME}/.plt_path" ]; then
#     PLT_PATH=$(cat ${HOME}/.plt_path)
# fi

# if [ -z "${PLT_PATH}" ] || ! [ "$(basename $PLT_PATH)" == "plt" ]; then
#     echo "PLT_PATH not avalable" >&2
#     unset y
#     read -p "true?: PLT_PATH=${PLT_PATH}" y
#     if [[ $y == "y" ]]; then
#         echo ${HOME}/REPOBARE/plt >"${HOME}/.plt_path"
#     else
#         echo "PLT_PATH not avalable create ${HOME}/.plt_path with path to plt; return 1" >&2
#         return 1
#     fi
# fi

unset REPO_PATH

if [ -f "${HOME}/.repo_path" ]; then
    REPO_PATH=$(cat ${HOME}/.repo_path)
fi

if [ -z "${REPO_PATH}" ] || ! [ "$(basename $REPO_PATH)" == "_repo" ]; then
    echo "REPO_PATH not avalable" >&2
    unset y
    read -p "true?: REPO_PATH=${PATH_CURR_REPO}" y
    if [[ $y == "y" ]]; then
        echo ${PATH_CURR_REPO} >${HOME}/.repo_path
    else
        echo "FONSH_PATH not avalable create ${HOME}/.repo_path with path to repo; return 1" >&2
        return 1
    fi

fi

IIFS=$IFS

# export PATH="${PATH_MAIN_REPO_FN_DIR}/dir_c_/_c_/_man/site/KeRi/1_Обзор_языка/res_cpp:$PATH"

PPWD="$(pwd)"

PLT_NAME=$(cat "${HOME}"/.plt_name)

. "${PATH_CONFIG_DIR}/env.sh"

eval "$(ssh-agent -s)"

TSH_book_path="${HOME}"/ProjectRepo_2/_repo/BOOK
# PATH_OS=${PATH_COMMUNIS}/Deploy_store
# PATH_FN="$PATH_OS/.qa/main_repo_fn" # path to pln
# PATH_TAG="$PATH_FN/_config/agno_tag" # path to tag tree

export IIFS

export MANOPT="-L ru"

. "$PATH_OS/.os/.sh/git-prompt.sh"

export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\033[35;40m\u@\H \w$(__git_ps1 " (%s)")\$\033[0m '

if [ -f ~/.bash_aliases ]; then

    . ~/.bash_aliases

fi

. "$PATH_OS/.qa/lib/user/0_ini.sh"

# c_up() {
#     echo -e "${CYAN}---c_up() \$1 = $1---${NORMAL}" #started functions
#     IFS=$IIFS

#     # cd_h "$1" "$filename" "$LINENO"
#     cd_h "$1"

#     local dir
#     dir=$(pwd)

#     for item in *; do
#         # echo -e "${GREEN}\$item = $item${NORMAL}"             #print variable
#         # echo -e "${GREEN}\${item:0:1} = ${item:0:1}${NORMAL}" #print variable
#         local item_path=$dir/$item
#         if [ -f "$item_path" ] && [ "${item:0:1}" != "_" ]; then
#             . "$item_path"
#             elif [ -d "$item_path" ] && [ "${item:0:1}" != "_" ]; then

#             c_up "$item_path"

#         fi

#     done
# }

c_up() {
    echo -e "${CYAN}---c_up() \$1 = $1---${NORMAL}" #started functions
    IFS=$IIFS

    # cd_h "$1" "$filename" "$LINENO"
    cd_h "$1"

    local dir
    dir=$(pwd)

    for item in *; do

        local item_path=$dir/$item
        if [ -f "$item_path" ] && [ "${item:0:1}" != "_" ] && [ "${item##*.}" = sh ]; then
            # echo "---------- "${item##*.}" ---------------"
            # if [ -f "$item_path" ] && [ "${item:0:1}" != "_" ]; then
            # echo "$item_path"
            # read -r e
            . "$item_path"

        elif
            [ -d "$item_path" ] && [ "${item:0:1}" != "_" ]
        then

            c_up "$item_path"

        fi

    done
}

export c_up

c_test() {
    # echo -e "${CYAN}---c_test() \$1 = $1---${NORMAL}" #started functions
    # IFS=$IIFS

    # cd_h "$1" "$filename" "$LINENO"
    cd "$1"

    local dir
    dir=$(pwd)

    for item in *; do

        local item_path=$dir/$item
        if [ -f "$item_path" ] && [ "${item:0:1}" != "_" ] && [ "${item##*.}" = "tst" ]; then
            # echo "---------- "$item_path" ---------------"
            . "$item_path"
        elif [ -d "$item_path" ] && [ "${item:0:1}" != "_" ]; then

            c_test "$item_path"

        fi

    done
}

rr() {
    echo -e "${CYAN}--- rr() ---${NORMAL}" #started functions

    echo -e "${ULINE} exec "file://${HOME}/.bashrc "${NORMAL}" #repit commands
    exec "${HOME}/.bashrc"
}

tst_bash() {

    c_test "$PATH_OS/.qa/" 1>/dev/null
    c_test "$PATH_OS/.os/" 1>/dev/null
    c_test "$PATH_OS/.os/.sh/" 1>/dev/null
    c_test "$PATH_OS/" 1>/dev/null

}

c_up "${HOME}"/start/fonsh/actio
c_up "${HOME}"/start/plt/actio

c_up "$PATH_OS/.qa/"
c_up "$PATH_OS/.os/"
c_up "$PATH_OS/.os/.sh/"
c_up "$PATH_OS/"

if [[ -d ${FONSH_PATH} ]]; then
    c_up "${FONSH_PATH}/actio"
fi

if [[ -d ${PLT_PATH} ]]; then
    c_up "${PLT_PATH}/actio"
fi

# if [ $HOSTTYPE == "altlinux" ]; then
#     code(){
#         codium
#     }
# fi

up_plt_cogos_

upgit
e_git

# echo -e "${BLUE}--- cp -rf ${PATH_COMMUNIS}/Deploy_store/plt_bd/main_store/sys_stores ${PATH_MY_ONE}/ ---${NORMAL}"
# cp -rf ${PATH_COMMUNIS}/Deploy_store/plt_bd/main_store/sys_stores ${PATH_MY_ONE}/
wrp_fifs2_ cp -rf ${PATH_COMMUNIS}/Deploy_store/plt_bd/main_store/sys_stores ${PATH_MY_ONE}/ -cd1 -cd2

add_PATH_
# ce_

cd_s "$PPWD" "$filename" "$LINENO"

#######
# . "${HOME}/.cargo/env"
___MY_VMOPTIONS_SHELL_FILE="${HOME}/.jetbrains.vmoptions.sh"
if [ -f "${___MY_VMOPTIONS_SHELL_FILE}" ]; then . "${___MY_VMOPTIONS_SHELL_FILE}"; fi
